package com.gx.auth.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gx.auth.dao.RoleFunctionInterfaceDAO;
import com.gx.auth.entity.Function;
import com.gx.auth.entity.Role;
import com.gx.auth.entity.RoleFunction;

public class RoleFunctionInterfaceImplDAO implements RoleFunctionInterfaceDAO {
	private Connection conn = null;

	public RoleFunctionInterfaceImplDAO(Connection conn) {
		this.conn = conn;
	}

	@Override
	public int insert(RoleFunction t) throws SQLException {
		String sql = "insert into auth_role_function (r_id,f_id,status,createtime,updatetime) values (?,?,?,datetime('now', 'localtime'),datetime('now', 'localtime')) ";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setInt(1, t.getRid());
		pst.setInt(2, t.getFid());
		pst.setBoolean(3, t.getStatus());
		int count = pst.executeUpdate();
		pst.close();
		return count;
	}

	@Override
	public int delete(RoleFunction t) throws SQLException {
		if (t.getRid() == null && t.getFid() != null) {
			String sql = "delete from auth_role_function where f_id = ? ";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setInt(1, t.getFid());
			int count = pst.executeUpdate();
			pst.close();
			return count;
		} else if (t.getRid() != null && t.getFid() == null) {
			String sql = "delete from auth_role_function where r_id = ? ";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setInt(1, t.getRid());
			int count = pst.executeUpdate();
			pst.close();
			return count;
		} else {
			String sql = "delete from auth_role_function where r_id = ? and f_id=? ";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setInt(1, t.getRid());
			pst.setInt(2, t.getFid());
			int count = pst.executeUpdate();
			pst.close();
			return count;
		}

	}

	@Override
	public int update(RoleFunction t) throws SQLException {
		String sql = "update auth_role_function set status = ?,updatetime = datetime('now', 'localtime') where r_id =? and f_id =? ";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setBoolean(1, t.getStatus());
		pst.setInt(2, t.getRid());
		pst.setInt(3, t.getFid());
		int count = pst.executeUpdate();
		pst.close();
		return count;
	}

	@Override
	public List<RoleFunction> queryAll() throws SQLException {
		List<RoleFunction> ls = new ArrayList<>();
		String sql = "select * from auth_role_function ";
		PreparedStatement pst = conn.prepareStatement(sql);
		ResultSet rs = pst.executeQuery();
		while (rs.next()) {
			ls.add(saveResult(rs));
		}
		rs.close();
		pst.close();
		return ls;
	}

	@Override
	public List<RoleFunction> query(Function f) throws SQLException {
		List<RoleFunction> ls = new ArrayList<>();
		String sql = "select * from auth_role_function where  f_id in (select f_id from auth_function where f_name = ?) ";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1, f.getFname());
		ResultSet rs = pst.executeQuery();
		while (rs.next()) {
			ls.add(saveResult(rs));
		}
		rs.close();
		pst.close();
		return ls;
	}

	@Override
	public List<RoleFunction> query(Role r) throws SQLException {
		List<RoleFunction> ls = new ArrayList<>();
		String sql = "select * from auth_role_function where  r_id in(select r_id from auth_role where r_name = ?) ";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1, r.getRname());
		ResultSet rs = pst.executeQuery();
		while (rs.next()) {
			ls.add(saveResult(rs));
		}
		rs.close();
		pst.close();
		return ls;
	}

	@Override
	public RoleFunction query(Integer rid, Integer fid) throws SQLException {
		RoleFunction rfc = new RoleFunction();
		String sql = "select * from auth_role_function where r_id = ? and f_id = ?";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setInt(1, rid);
		pst.setInt(2, fid);
		ResultSet rs = pst.executeQuery();
		if (rs.next()) {
			rfc = saveResult(rs);
		}
		rs.close();
		pst.close();
		return rfc;
	}

	public RoleFunction saveResult(ResultSet rs) throws SQLException {
		RoleFunction rfc = new RoleFunction();
		rfc.setRfid(rs.getInt("rf_id"));
		rfc.setRid(rs.getInt("r_id"));
		rfc.setFid(rs.getInt("f_id"));
		rfc.setStatus(rs.getBoolean("status"));
		rfc.setCreatetime(rs.getString("createtime"));
		rfc.setUpdatetime(rs.getString("updatetime"));
		return rfc;
	}
}
